home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows (5th Edition) / Programming Windows, 5th ed. - Companion CD (097-0002183)(1999).iso / Chap10 / IconDemo / IconDemo.c next >
Encoding:
C/C++ Source or Header  |  1998-10-09  |  2.9 KB  |  93 lines

  1. /*------------------------------------------
  2.    ICONDEMO.C -- Icon Demonstration Program
  3.                  (c) Charles Petzold, 1998
  4.   ------------------------------------------*/
  5.  
  6. #include <windows.h>
  7. #include "resource.h"
  8.  
  9. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  10.  
  11. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  12.                     PSTR szCmdLine, int iCmdShow)
  13. {
  14.      TCHAR    szAppName[] = TEXT ("IconDemo") ;
  15.      HWND     hwnd ;
  16.      MSG      msg ;
  17.      WNDCLASS wndclass ;
  18.  
  19.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  20.      wndclass.lpfnWndProc   = WndProc ;
  21.      wndclass.cbClsExtra    = 0 ;
  22.      wndclass.cbWndExtra    = 0 ;
  23.      wndclass.hInstance     = hInstance ;
  24.      wndclass.hIcon         = LoadIcon (hInstance, MAKEINTRESOURCE (IDI_ICON)) ;
  25.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  26.      wndclass.hbrBackground = GetStockObject (WHITE_BRUSH) ;
  27.      wndclass.lpszMenuName  = NULL ;
  28.      wndclass.lpszClassName = szAppName ;
  29.  
  30.      if (!RegisterClass (&wndclass))
  31.      {
  32.           MessageBox (NULL, TEXT ("This program requires Windows NT!"),
  33.                       szAppName, MB_ICONERROR) ;
  34.           return 0 ;
  35.      }
  36.      
  37.      hwnd = CreateWindow (szAppName, TEXT ("Icon Demo"),
  38.                           WS_OVERLAPPEDWINDOW,
  39.                           CW_USEDEFAULT, CW_USEDEFAULT,
  40.                           CW_USEDEFAULT, CW_USEDEFAULT,
  41.                           NULL, NULL, hInstance, NULL) ;
  42.      
  43.      ShowWindow (hwnd, iCmdShow) ;
  44.      UpdateWindow (hwnd) ;
  45.      
  46.      while (GetMessage (&msg, NULL, 0, 0))
  47.      {
  48.           TranslateMessage (&msg) ;
  49.           DispatchMessage (&msg) ;
  50.      }
  51.      return msg.wParam ;
  52. }
  53.  
  54. LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  55. {
  56.      static HICON hIcon ;
  57.      static int   cxIcon, cyIcon, cxClient, cyClient ;
  58.      HDC          hdc ;
  59.      HINSTANCE    hInstance ;
  60.      PAINTSTRUCT  ps ;
  61.      int          x, y ;
  62.      
  63.      switch (message)
  64.      {
  65.      case WM_CREATE :
  66.           hInstance = ((LPCREATESTRUCT) lParam)->hInstance ;
  67.           hIcon = LoadIcon (hInstance, MAKEINTRESOURCE (IDI_ICON)) ;
  68.           cxIcon = GetSystemMetrics (SM_CXICON) ;
  69.           cyIcon = GetSystemMetrics (SM_CYICON) ;
  70.           return 0 ;
  71.           
  72.      case WM_SIZE :
  73.           cxClient = LOWORD (lParam) ;
  74.           cyClient = HIWORD (lParam) ;
  75.           return 0 ;
  76.           
  77.      case WM_PAINT :
  78.           hdc = BeginPaint (hwnd, &ps) ;
  79.           
  80.           for (y = 0 ; y < cyClient ; y += cyIcon)
  81.                for (x = 0 ; x < cxClient ; x += cxIcon)
  82.                     DrawIcon (hdc, x, y, hIcon) ;
  83.                
  84.                EndPaint (hwnd, &ps) ;
  85.                return 0 ;
  86.                
  87.      case WM_DESTROY :
  88.           PostQuitMessage (0) ;
  89.           return 0 ;
  90.      }
  91.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  92. }
  93.